package com.cskaoyan.controller;

import com.cskaoyan.model.User;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @PROJECT_NAME: git_homework
 * @DESCRIPTION: git作业
 * @Author: group 2
 * @DATE: 2021/10/16 21:45
 */
@RestController // 统一返回Json格式数据
@RequestMapping("/api/v1/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     * 测试用例：http://localhost:8080/api/v1/user/login?username=peiqi&password=123456
     *
     * @return
     */
    @RequestMapping("/login")
    public BaseRespVo login(User user) {

        return BaseRespVo.ok(userService.login(user));
    }

    /**
     * 用户注册接口
     * 测试用例：http://localhost:8080/api/v1/user/register?username=qiaozhi&password=666666
     *
     * @return
     */
    @RequestMapping("/register")
    public BaseRespVo register(User user) {
        userService.register(user);
        return BaseRespVo.ok();
    }
    /**
     * 根据id查询用户
     * 测试用例：http://localhost:8080/api/v1/user/searchId=1
     *
     * @return
     */
    @RequestMapping("/searchId")
    public BaseRespVo searchId(Integer id) {

        return BaseRespVo.ok(userService.selectByPrimaryKey(id));
    }

    /**
     * 查询所有用户接口
     * 测试用例：http://localhost:8080/api/v1/user/getAllUser
     *
     * @return
     */
    @RequestMapping("/getAllUser")
    public BaseRespVo getAllUser() {
        
        return BaseRespVo.ok(userService.getAllUser());
    }

    /**
     * 用户的模糊查询根据用户名
     * 测试用例：http://localhost:8080/api/v1/user/getUserLikeUsername?username=q
     *
     * @Author tangjian
     * @DATE 2021/10/18 9:29
     */
    @RequestMapping("/getUserLikeUsername")
    public BaseRespVo getUserLikeUsername(String username){
        List<User> userLikeUsername = userService.getUserLikeUsername(username);

        return BaseRespVo.ok(userLikeUsername);
    }

}
